using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(ChosTIS.TetrisItemGrid))]
public class DepositoryGridEditor : Editor
{
    private int _width;
    private int _height;
    private float _tileWidth;
    private float _tileHeight;

    private Color bgColor = new Color(0.22f, 0.17f, 0.13f, 1f); // 深棕色
    private Color borderColor = new Color(0.35f, 0.25f, 0.15f, 1f); // 较亮棕色
    private Color goldColor = new Color(0.85f, 0.68f, 0.25f, 1f); // 金色

    private void OnEnable()
    {
        var grid = (ChosTIS.TetrisItemGrid)target;
        _width = grid.gridSizeWidth;
        _height = grid.gridSizeHeight;
        _tileWidth = grid.TileSizeWidth;
        _tileHeight = grid.TileSizeHeight;
    }

    public override void OnInspectorGUI()
    {
        var grid = (ChosTIS.TetrisItemGrid)target;

        // 背景块
        Rect rect = GUILayoutUtility.GetRect(0, 180, GUILayout.ExpandWidth(true));
        EditorGUI.DrawRect(rect, bgColor);

        GUILayout.BeginArea(rect);

        // 古朴金色大标题
        GUIStyle titleStyle = new GUIStyle(EditorStyles.boldLabel);
        titleStyle.fontSize = 18;
        titleStyle.normal.textColor = goldColor;
        titleStyle.alignment = TextAnchor.MiddleCenter;
        GUILayout.Space(8);
        GUILayout.Label("╔═ 仓库参数设置 ═╗", titleStyle);
        GUILayout.Space(8);

        // 分割线
        DrawDivider();

        // 仓库尺寸
        GUIStyle labelStyle = new GUIStyle(EditorStyles.label);
        labelStyle.fontSize = 13;
        labelStyle.normal.textColor = borderColor;
        GUILayout.Label("仓库尺寸（格子数）", labelStyle);
        _width = EditorGUILayout.IntField("宽度（格）", _width);
        _height = EditorGUILayout.IntField("高度（格）", _height);

        GUILayout.Space(6);
        DrawDivider();

        // 单元格像素大小
        GUILayout.Label("单元格像素大小", labelStyle);
        _tileWidth = EditorGUILayout.FloatField("单元格宽度", _tileWidth);
        _tileHeight = EditorGUILayout.FloatField("单元格高度", _tileHeight);

        GUILayout.Space(10);

        // 古朴大气按钮
        GUIStyle btnStyle = new GUIStyle(GUI.skin.button);
        btnStyle.fontSize = 14;
        btnStyle.fontStyle = FontStyle.Bold;
        btnStyle.normal.textColor = goldColor;
        btnStyle.fixedHeight = 32;
        btnStyle.margin = new RectOffset(20, 20, 0, 0);

        if (GUILayout.Button("应用新尺寸和单元格大小", btnStyle))
        {
            Undo.RecordObject(grid, "Change Depository Size and Cell Size");
            grid.gridSizeWidth = _width;
            grid.gridSizeHeight = _height;
            if (grid.gridConfig != null)
            {
                grid.gridConfig.tileSizeWidth = _tileWidth;
                grid.gridConfig.tileSizeHeight = _tileHeight;
                EditorUtility.SetDirty(grid.gridConfig);
            }
            var method = grid.GetType().GetMethod("Initialize", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
            method.Invoke(grid, new object[] { _width, _height });
            EditorUtility.SetDirty(grid);
        }

        GUILayout.Space(8);
        GUILayout.Label("╚══════════════╝", titleStyle);

        GUILayout.EndArea();
    }

    private void DrawDivider()
    {
        Rect r = GUILayoutUtility.GetRect(1, 2, GUILayout.ExpandWidth(true));
        EditorGUI.DrawRect(new Rect(r.x, r.y, r.width, 2), borderColor);
        GUILayout.Space(2);
    }
} 